package com.mall.orders.server.service.impl;

import com.mall.common.security.utils.SecurityUtils;
import com.mall.orders.common.domain.Chargeback;
import com.mall.orders.common.domain.Orders;
import com.mall.orders.common.domain.dto.OrdersDto;
import com.mall.orders.common.domain.vo.OrdersVo;
import com.mall.orders.server.mapper.OrdersMapper;
import com.mall.orders.server.service.OrdersService;
import com.mall.system.common.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class OrdersServiceImpl implements OrdersService {

    @Autowired
    private OrdersMapper ordersMapper;

    /**
     * 根据订单主键查询订单
     * @param id 订单主键
     * @return
     */
    @Override
    public OrdersVo selectOrdersById(Long id) {
        OrdersVo ordersVo = ordersMapper.selectOrdersById(id);

        //获取当前登录人  获取当前登录人手机号
        String username = SecurityUtils.getLoginUser().getUsername();
        ordersVo.setUserPhone("14785214545");
        ordersVo.setUserName(username);
        return ordersVo;
    }

    /**
     * 查询订单列表
     *
     * @param ordersDto 订单
     * @return 订单
     */
    @Override
    public List<OrdersVo> selectOrdersList(OrdersDto ordersDto)
    {
        List<OrdersVo> ordersVos = ordersMapper.selectOrdersList(ordersDto);
        //获取当前登录人
        String username = SecurityUtils.getLoginUser().getUsername();
        for (OrdersVo ordersVo : ordersVos) {
            ordersVo.setUserName(username);
        }
        return ordersVos;
    }


    /**
     * 新增订单
     *
     * @param orders 订单
     * @return 结果
     */
    @Override
    public int insertOrders(Orders orders)
    {
        return ordersMapper.insertOrders(orders);
    }

    /**
     * 修改订单
     *
     * @param orders 订单
     * @return 结果
     */
    @Override
    public int updateOrders(Orders orders)
    {
        return ordersMapper.updateOrders(orders);
    }

    /**
     * 批量删除订单
     *
     * @param ids 需要删除的订单主键
     * @return 结果
     */
    @Override
    public int deleteOrdersByIds(Long[] ids)
    {
        return ordersMapper.deleteOrdersByIds(ids);
    }

    /**
     * 删除订单信息
     *
     * @param id 订单主键
     * @return 结果
     */

    @Override
    public void deleteOrdersById(Long id)
    {
        ordersMapper.deleteOrdersById(id);
    }

    @Override
    public void updateCancelStatus(Orders orders) {
        orders.setOrderStatus(5L);
        ordersMapper.updateCancelStatus(orders);
    }

    /**
     * 修改收货地址
     * @param orders
     */
    @Override
    public void updateAddr(OrdersVo orders) {
        ordersMapper.updateAddr(orders);
    }

    /**
     * 修改总价
     * @param orders
     */
    @Override
    public void updateTotalMoney(OrdersVo orders) {
        ordersMapper.updateTotalMoney(orders);
    }

    @Override
    public void updateDeliverStatus(Long id) {
        Orders orders = new Orders();
        orders.setOrderStatus(3L);
        ordersMapper.updateDeliverStatus(id);
    }


}
